package hanshunping.project.chatRoom.chatClient.client.service;

import hanshunping.project.chatRoom.common.Message;
import hanshunping.project.chatRoom.common.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class ConnectThread extends Thread{
    private Socket socket;

    public ConnectThread(Socket socket) {
        this.socket = socket;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while(true){
            try {
                System.out.println("客户端线程，等待读取从服务端发送的消息");
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message ms = (Message) objectInputStream.readObject();

                switch (ms.getMesType()){
                    case MessageType.MESSAGE_RETURN_ONLINE:
                        String[] onlines = ms.getContent().split(" ");
                        System.out.println("\n==============用户列表为==============");
                        for(int i = 0;i<onlines.length;i++){
                            System.out.println("用户："+onlines[i]);
                        }
                        break;
                    case MessageType.MESSAGE_MES:
                        System.out.println(ms.getSender()+"对"+ms.getSender()+"说："+ms.getContent());
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
